<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="用户名称" prop="keywords">
        <el-input
            v-model="queryParams.keywords"
            placeholder="请输入评论或回复人昵称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['news:comment:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="linkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <el-table-column prop="avatar" align="center" width="150" label="头像">
        <template  #default="scope">
          <el-avatar shape="square" :size="50" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="nickname" align="center" width="200" label="评论用户" />
      <el-table-column prop="replyNickname" align="center" width="200" label="回复用户" />
      <el-table-column prop="articleTitle" align="center" width="250" label="所属文章" />
      <el-table-column prop="content" align="center" width="300" label="内容">
        <template  #default="scope">
          <span v-html="scope.row.content" class="comment-content" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" >
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="danger" @click="handleDelete(scope.row)" v-hasPermi="['news:comment:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="Category">
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import {addCategory, delCategory, getCategory, listCategory, updateCategory} from "@/api/blog/category";
import SvgIcon from "@/components/SvgIcon/index.vue";
import IconSelect from "@/components/IconSelect/index.vue";
import {addLink, delLink, getLink, listLink, updateLink} from "@/api/blog/friendLink";
import {fetchComment} from "@/api/blog/comment";

const { proxy } = getCurrentInstance();

const showChooseIcon = ref(false);
const iconSelectRef = ref(null);
const linkList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const names = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    title: null,
    pageNum: 1,
    pageSize: 10
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  fetchComment(queryParams.value).then(response => {
    linkList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    name: "",
    sort: ""
  };
  proxy.resetForm("tagRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  names.value = selection.map(item => item.name);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加友情链接";
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getLink(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改友情链接";
  });
}
// 上传成功回调
function handleUploadSuccess(res, file) {
  if (res.code === 200) {
    form.value.avatar = res.url;
  }
}

function handleDeletePic(res, file) {
  form.value.avatar = null;
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["tagRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateLink(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addLink(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value
  const name = row.name || names.value
  proxy.$modal.confirm('是否确认删除网站名称为"' + name + '"的数据项？').then(function() {
    return delLink(id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 展示下拉图标 */
function showSelectIcon() {
  iconSelectRef.value.reset();
  showChooseIcon.value = true;
}
/** 选择图标 */
function selected(name) {
  form.value.icon = name;
  showChooseIcon.value = false;
}
/** 图标外层点击隐藏下拉列表 */
function hideSelectIcon(event) {
  var elem = event.relatedTarget || event.srcElement || event.target || event.currentTarget;
  var className = elem.className;
  if (className !== "el-input__inner") {
    showChooseIcon.value = false;
  }
}
function onClick(url) {
  window.open(url)
}
getList();
</script>
